package com.yang.controller;

import com.yang.service.UploadService;
import org.apache.commons.fileupload.FileItem;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import java.util.*;

@Controller
public class UploatExcelController {

    @Resource
    UploadService uploadService;

    private static Logger logger = Logger.getLogger(UploatExcelController.class);

    @RequestMapping(value = "admin/upload_user_information")
    @ResponseBody
    public Map<String,Object> uploadUser(HttpServletRequest request){

        Map<String,Object> map = new HashMap<String, Object>();
        CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if(!resolver.isMultipart(request)){
            map.put("status","error");
            map.put("info","上传为空");
            return map;
        }
        MultipartHttpServletRequest multipartHttpServletRequest = resolver.resolveMultipart(request);
        Iterator<String> iterable = multipartHttpServletRequest.getFileNames();
        List<MultipartFile> multipartFiles = new ArrayList<MultipartFile>();
        while(iterable.hasNext()){
            MultipartFile file = multipartHttpServletRequest.getFile(iterable.next());
            multipartFiles.add(file);
        }
        map = uploadService.readExcel(multipartFiles);
        return map;
    }
}
